﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    public class ProductStorageSearch : SkinnedWebControl
    {
        private TextBox tbxModel;
        private TextBox tbxProduct;
        private TextBox tbxBrand;
        //private ProductDropDownList ddlProducts;
        //private TreeViewDropDownList ddlBrand;
        private IButtonControl btnConfirm;
        public event Func<string,string,string,string> SearchUrlBuilderHandle;

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            tbxModel = (TextBox)skin.FindControl("tbxModel");
            tbxProduct = (TextBox)skin.FindControl("tbxProduct");
            tbxBrand = (TextBox)skin.FindControl("tbxBrand");
            //ddlProducts = (ProductDropDownList)skin.FindControl("ddlProducts");
            //ddlBrand = (TreeViewDropDownList)skin.FindControl("ddlBrand");
            btnConfirm = (IButtonControl)skin.FindControl("btnConfirm");

            btnConfirm.Click += new EventHandler(btnConfirm_Click);

            if (!Page.IsPostBack)
            {
                string[] searchGroup = SiteUrls.AnalyzeStorageSearchKey(SiteContext.Current.SearchKey);

                tbxModel.Text = searchGroup[0];
                tbxProduct.Text = searchGroup[1];
                tbxBrand.Text = searchGroup[2];
            }
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            string url = Page.Urls.ProductStorageSearch(
                SiteContext.Current.EnableDelButton,
                SiteContext.Current.EnableTopButton,
                SiteContext.Current.EnableHotButton,
                this.tbxModel.Text.Trim(),
                this.tbxProduct.Text.Trim(),
                this.tbxBrand.Text.Trim());
            
            if (SearchUrlBuilderHandle != null)
                url = SearchUrlBuilderHandle(
                    this.tbxModel.Text.Trim(), 
                    this.tbxProduct.Text.Trim(), 
                    this.tbxBrand.Text.Trim());

            Page.Response.Redirect(url, true);
        }

        protected override string DefaultSkinFilename
        {
            get { return "ProductStorage/ProductStorageSearch.ascx"; }
        }
    }
}
